package com.trm.role.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.common.servlet.CommonServlet;
import com.trm.role.dao.RoleDao;

public class RoleServlet extends CommonServlet {

    /**
     * 角色列表
     * 
     * @param request
     * @param response
     */
    public void getRoleList(HttpServletRequest request, HttpServletResponse response) {
        String page = request.getParameter("page"); // 页码
        RoleDao roleDao = new RoleDao();
        if (page != null && !"".equals(page)) {
            roleDao.setPage(Integer.valueOf(page));
        }
        // 获得用户数量
        int count = roleDao.getRoleCount();
        // 页数量
        int pageNumber = count / roleDao.getPageSize();
        if (count % roleDao.getPageSize() > 0) {
            pageNumber++;
        }
        if (pageNumber == 0) {
            pageNumber = 1;
        }
        List<Map<String, Object>> roleList = roleDao.getRolePageList();
        request.setAttribute("roleList", roleList);
        request.setAttribute("page", roleDao.getPage());
        request.setAttribute("pageNumber", pageNumber);
        try {
            request.getRequestDispatcher("/manager/roleManager.jsp").forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 编辑页面
     * 
     * @param request
     * @param response
     */
    public void add(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        String[] rights = new String[] {};
        String[] properties = new String[] {};
        if (id != null && !"".equals(id)) {
            RoleDao roleDao = new RoleDao();
            Map<String, Object> role = roleDao.getRoleById(id);
            request.setAttribute("role1", role);
            // 处理权限信息
            String right = (String) role.get("right");
            if (right != null && !"".equals(right)) {
                rights = right.split(";");
            }
            // 处理属性信息
            String property = (String) role.get("property");
            if (property != null && !"".equals(property)) {
                properties = property.split(";");
            }
        }
        RoleDao roleDao = new RoleDao();
        // 取可查看角色信息
        List<Map<String, Object>> roleList = roleDao.getRoleList();
        for (String right : rights) {
            for (Map<String, Object> role1 : roleList) {
                if (right.equals(role1.get("id").toString())) {
                    role1.put("checked", "checked");
                }
            }
            if ("x".equals(right)) {
                request.setAttribute("manager", "checked");
            }
        }
        request.setAttribute("roleList", roleList);
        // 取属性信息
        List<Map<String, Object>> propertyList = roleDao.getProperties();
        for (String property : properties) {
            for (Map<String, Object> property1 : propertyList) {
                if (property.equals(property1.get("property").toString())) {
                    property1.put("checked", "checked");
                }
            }
        }
        request.setAttribute("propertyList", propertyList);
        // 页面跳转
        try {
            request.getRequestDispatcher("/manager/addRole.jsp").forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 保存角色
     * 
     * @param request
     * @param response
     */
    public void saveRole(HttpServletRequest request, HttpServletResponse response) {
        // id
        String id = request.getParameter("id");
        // 角色名称
        String role = request.getParameter("role");
        // 角色权限
        String[] rights = request.getParameterValues("right");
        // 角色属性
        String[] properties = request.getParameterValues("property");
        // 处理权限字符
        String right = "";
        if (rights != null && rights.length > 0) {
            for (int i = 0; i < rights.length; i++) {
                right += rights[i] + ";";
            }
        }
        // 处理属性字符
        String property = "";
        if (properties != null && properties.length > 0) {
            for (int j = 0; j < properties.length; j++) {
                property += properties[j] + ";";
            }
        }
        // 封装参数到map
        Map<String, String> params = new HashMap<String, String>();
        params.put("role", role);
        params.put("right", right);
        params.put("property", property);
        RoleDao roleDao = new RoleDao();
        if (id == null || "".equals(id)) {
            int result = roleDao.addRole(params);
        } else {
            params.put("id", id);
            int result = roleDao.updateRoleById(params);
        }
        try {
            response.sendRedirect("RoleServlet?action=getRoleList");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除角色
     * 
     * @param request
     * @param response
     */
    public void deleteRole(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        RoleDao roleDao = new RoleDao();
        int result = roleDao.deleteRoleById(id);
        try {
            response.sendRedirect("RoleServlet?action=getRoleList");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
